package cn.wolfcode.car.business.web.controller;


import cn.wolfcode.car.base.domain.User;
import cn.wolfcode.car.base.service.IUserService;
import cn.wolfcode.car.business.domain.BpmnInfo;
import cn.wolfcode.car.business.domain.ServiceItem;
import cn.wolfcode.car.business.query.ServiceItemQuery;
import cn.wolfcode.car.business.service.IServiceItemServicePlus;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.web.AjaxResult;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.xmlbeans.impl.xb.xsdschema.Public;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.Banner;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

/**
 * 服务单项控制器
 */
@Controller
@RequestMapping("business/serviceItem")
public class ServiceItemController {
    //模板前缀
    private static final String prefix = "business/serviceItem/";

    @Autowired
    private IServiceItemServicePlus serviceItemServicePlus;

    @Autowired
    private IUserService userService;

    //页面------------------------------------------------------------
    //列表
    @RequiresPermissions("system:serviceItem:view")
    @RequestMapping("/listPage")
    public String listPage(){
        return prefix + "list";
    }

    @RequiresPermissions("system:serviceItem:add")
    @RequestMapping("/addPage")
    public String addPage(){
        return prefix + "add";
    }


    @RequiresPermissions("system:serviceItem:edit")
    @RequestMapping("/editPage")
    public String editPage(Long id, Model model){
        model.addAttribute("serviceItem", serviceItemServicePlus.getById(id));
        return prefix + "edit";
    }

    //数据-----------------------------------------------------------
    //列表
    @RequiresPermissions("system:serviceItem:list")
    @RequestMapping("/query")
    @ResponseBody
    public TablePageInfo<ServiceItem> query(ServiceItemQuery qo){
        return serviceItemServicePlus.query(qo);
    }


    //新增
    @RequiresPermissions("system:serviceItem:add")
    @RequestMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ServiceItem serviceItem){
        /*//在进行添加操作时，先判断是不是套餐
        //是套餐需要进行审核，把状态设置为初始化
        if(ServiceItem.CARPACKAGE_YES.equals(serviceItem.getCarPackage())){
            serviceItem.setAuditStatus(ServiceItem.AUDITSTATUS_INIT);
        }else{
            //非套餐，把状态设置为默认值:无需审核
            serviceItem.setAuditStatus(ServiceItem.AUDITSTATUS_NO_REQUIRED);
        }
        //添加完成首先需要让该数据为下架状态
        //由于添加页面没有给时间、销售状态、审核状态等值进行填写，需要手动进行赋值或给默认值进行补齐
        serviceItem.setSaleStatus(ServiceItem.SALESTATUS_OFF);
        serviceItem.setCreateTime(new Date());
        serviceItemMapper.insert(serviceItem);*/

        //是套餐需要进行审核，把状态设置为初始化
        if(ServiceItem.CARPACKAGE_YES.equals(serviceItem.getCarPackage())){
            serviceItem.setAuditStatus(ServiceItem.AUDITSTATUS_INIT);
        }else{
            //非套餐，把状态设置为默认值:无需审核
            serviceItem.setAuditStatus(ServiceItem.AUDITSTATUS_NO_REQUIRED);
        }
        //添加完成首先需要让该数据为下架状态
        //由于添加页面没有给时间、销售状态、审核状态等值进行填写，需要手动进行赋值或给默认值进行补齐
        UpdateWrapper<ServiceItem> wrapper = new UpdateWrapper<>();
        wrapper.set("sale_status",ServiceItem.SALESTATUS_OFF)
                .set("create_time",new Date());
        serviceItemServicePlus.save(serviceItem);
        return AjaxResult.success();
    }

    //编辑
    @RequiresPermissions("system:serviceItem:edit")
    @RequestMapping("/edit")
    @ResponseBody
    public AjaxResult edit(ServiceItem serviceItem){
        /*//2.先查，在替换，最后再更新
        //如果是上架状态，不能进行修改
        ServiceItem st = this.get(serviceItem.getId());
        if(ServiceItem.SALESTATUS_ON.equals(st.getSaleStatus())){
            throw new BusinessException("该服务项在上架中，不允许修改");
        }
        //如果是套餐，且没有审核通过，不能进行修改
        if(ServiceItem.CARPACKAGE_YES.equals(st.getCarPackage())
                && ServiceItem.AUDITSTATUS_AUDITING.equals(st.getAuditStatus())){
            throw new BusinessException("审核中，不能进行修改");
        }
        //如果是下架状态,并且套餐，且审核通过了，再重新进行编辑，需要把审核状态设为初始状态
        if(ServiceItem.CARPACKAGE_YES.equals(st.getCarPackage())
                && ServiceItem.AUDITSTATUS_APPROVED.equals(st.getAuditStatus())){
            st.setAuditStatus(ServiceItem.AUDITSTATUS_INIT);
        }else {
            //是什么审核状态就返回什么审核状态
            serviceItem.setAuditStatus(st.getAuditStatus());
        }
        //把销售状态设置为下架
        serviceItem.setSaleStatus(ServiceItem.SALESTATUS_OFF);
        serviceItem.setCreateTime(serviceItem.getCreateTime());*/

        ServiceItem st = serviceItemServicePlus.getById(serviceItem.getId());
        if(ServiceItem.SALESTATUS_ON.equals(st.getSaleStatus())){
            throw new BusinessException("该服务项在上架中，不允许修改");
        }
        if(ServiceItem.CARPACKAGE_YES.equals(st.getCarPackage())
                && ServiceItem.AUDITSTATUS_AUDITING.equals(st.getAuditStatus())){
            throw new BusinessException("审核中，不能进行修改");
        }
        if(ServiceItem.CARPACKAGE_YES.equals(st.getCarPackage())
                && ServiceItem.AUDITSTATUS_APPROVED.equals(st.getAuditStatus())){
            st.setAuditStatus(ServiceItem.AUDITSTATUS_INIT);
        }else {
            //是什么审核状态就返回什么审核状态
            serviceItem.setAuditStatus(st.getAuditStatus());
        }
        UpdateWrapper<ServiceItem> wrapper = new UpdateWrapper<>();
        wrapper.eq("id",serviceItem.getId())
                .set("sale_status",ServiceItem.SALESTATUS_OFF);
        serviceItemServicePlus.update(serviceItem,wrapper);
        return AjaxResult.success();
    }

    //删除
    @RequiresPermissions("system:serviceItem:remove")
    @RequestMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids){
        serviceItemServicePlus.removeById(ids);
        return AjaxResult.success();
    }

    @RequiresPermissions("system:serviceItem:saleOn")
    @RequestMapping("/saleOn")
    @ResponseBody
    public AjaxResult saleOn(Long id){
        serviceItemServicePlus.saleOn(id);
        return AjaxResult.success();
    }

    @RequiresPermissions("system:serviceItem:saleOff")
    @RequestMapping("/saleOff")
    @ResponseBody
    public AjaxResult saleOff(Long id){
        serviceItemServicePlus.saleOff(id);
        return AjaxResult.success();
    }


    //列表
    @RequiresPermissions("system:serviceItem:selectAllSaleOnList")
    @RequestMapping("/selectAllSaleOnList")
    @ResponseBody
    public TablePageInfo<ServiceItem> selectAllSaleOnList(ServiceItemQuery qo){
        //只查询已上架的服务单项
        qo.setSaleStatus(ServiceItem.SALESTATUS_ON);
        return serviceItemServicePlus.query(qo);
    }

    @RequiresPermissions("system:serviceItem:auditPage")
    @RequestMapping("/auditPage")
    public String auditPage(Long id, Model model){
        //获取服务单项
        ServiceItem serviceItem = serviceItemServicePlus.getById(id);
        model.addAttribute("serviceItem",serviceItem);
        //获取审核流程名称
        BpmnInfo bpmnInfo = serviceItemServicePlus.queryByBpmnType("car_package");
        model.addAttribute("bpmnInfo",bpmnInfo);
        //获取审核人
        List<User> directors = userService.queryByRoleKey("shopOwner");
        List<User> finances = userService.queryByRoleKey("financial");
        model.addAttribute("directors",directors);
        model.addAttribute("finances",finances);
        return prefix + "audit";
    }


   /* *//**
     * 启动流程实例
     * @return
     */
    @RequiresPermissions("system:serviceItem:startAudit")
    @RequestMapping("/startAudit")
    @ResponseBody
    public AjaxResult startAudit(Long id,Long bpmnInfoId,Long director,Long finance,String info){
        serviceItemServicePlus.startAudit(id,bpmnInfoId,director,finance,info);
        return AjaxResult.success();
    }



}
